package com.drug.service.impl;

import com.drug.domain.Result;
import com.drug.domain.Supplier;
import com.drug.mapper.SupplierMapper;
import com.drug.service.SupplierService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {
   @Autowired
   private SupplierMapper supplierMapper;
   //条件查询
    @Override
    public List<Supplier> selectSupplier(Supplier supplier) {
           // 监督机构名称
        if (supplier.getSurName()!=null&&supplier.getSurName()!=""){
            String surName = supplier.getSurName();
            supplier.setSurName(surName);
        }else {
            supplier.setSurName(null);
        }
        //    类别
        if (supplier.getSurCategory()!=null||supplier.getSurCategory().equals("")){
            String surCategory = supplier.getSurCategory();
            supplier.setSurCategory(surCategory);
        }else {
            supplier.setSurCategory(null);
        }
        //    邮政编码
        if (supplier.getSurPostalcode()!=null&&supplier.getSurPostalcode()!=""){
            String surPostalcode = supplier.getSurPostalcode();
            supplier.setSurPostalcode(surPostalcode);
        }else {
            supplier.setSurPostalcode(null);
        }
        //    法人代表姓名
        if (supplier.getSurLegalPersonname()!=null||supplier.getSurLegalPersonname()!=""){
            String surLegalPersonname= supplier.getSurLegalPersonname();
            supplier.setSurLegalPersonname(surLegalPersonname);
        }else {
            supplier.setSurLegalPersonname(null);
        }
        //    法人省份证
        if (supplier.getSurLegalpersonIdcard()!=null&&supplier.getSurLegalpersonIdcard()!=""){
            String surLegalpersonIdcard= supplier.getSurLegalpersonIdcard();
            supplier.setSurLegalpersonIdcard(surLegalpersonIdcard);
        }else {
            supplier.setSurLegalpersonIdcard(null);
        }
        //     email
        if (supplier.getSurEmail()!=null&&supplier.getSurEmail()!=""){
            String surEmail= supplier.getSurEmail();
            supplier.setSurEmail(surEmail);
        }else {
            supplier.setSurEmail(null);
        }
        //     是否有企业信用证
        if (supplier.getSurEnterpriseLetter()!=null||supplier.getSurEnterpriseLetter().equals("")){
            String surEnterpriseLetter= supplier.getSurEnterpriseLetter();
            supplier.setSurEnterpriseLetter(surEnterpriseLetter);
        }else {
            supplier.setSurEnterpriseLetter(null);
        }
        return supplierMapper.selectSupplier(supplier);
    }
    //分页查询
    @Override
    public Result pageSupplierFindAll(Integer page) {
        //使用分页插件
        PageHelper.startPage(page,5);
        Result result = new Result<>();
        List<Supplier> supervisoryBodyList = supplierMapper.pageSupplierFindAll();
        PageInfo<Supplier> pageInfo = new PageInfo<>(supervisoryBodyList);
        result.setData(pageInfo);
        return result;
    }


    //逻辑删除
    @Override
    public void deleteSupplier(Integer surId) {
        supplierMapper.deleteSupplier(surId);
    }
    //增加
    @Override
    public void insertSupplier(Supplier supplier) {
     supplierMapper.insertSupplier(supplier);
    }
    //修改
    @Override
    public List<Supplier> updateSupplier(Supplier supplier) {
        supplierMapper.updateSupplier(supplier);
        return null;
    }
	
	/**
     * 查询所有的生产企业
     * @return
     */
    @Override
    public Result selectAll() {
        Result result = new Result();
        List<Supplier> suppliers = supplierMapper.selectAll();
        result.setData(suppliers);
        return result;
    }

    /**
     * 查询供货商名字
     * @param surName
     * @return
     */
    @Override
    public Result selectSupplier1(String surName) {
        Result result = new Result();
        List<Supplier> suppliers = supplierMapper.selectSupplier1(surName);
        for (int i = 0; i < suppliers.size(); i++) {
            if (surName.equals(surName)){
                result.setMsg("供货商已存在");
                result.setCode(0);
                break;
            }else {
                result.setMsg("供货商可用");
                result.setCode(1);
            }
        }
        return result;
    }

}
